package ti.ml.car;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.color.ColorSpace;
import javax.swing.ImageIcon;
import ti.ml.car.domain.Block;

/**
 *
 * @author Sander
 */
public class Road {

    private static int nrofblocks = 20;
    private static int blocksize = 40;
    private final String grassSrc = "/images/grass.png";
    private int scrsize;
    private Image grass;
    private Color roadColor, gridLineColor, obstableColor, asphalt;
    //private short[] screendata;
    private int[][] screendata;
    final int[][] leveldata2 = {
        //volgorde: N-E-S-W-center
        //moet 20 op 20 groot worden
        //1
        Block.N_SWC(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.NES_C(),
        Block.N__W_(), Block.N____(), Block.NE___(), Block.N_SWC(), Block.N_S_C(), Block.NES_C(), Block.N__W_(), Block.N____(), Block.NE___(), Block.NE_WC(),
        //2
        Block.N__W_(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(),
        Block._____(), Block._____(), Block._____(), Block.N____(), Block.N____(), Block.N____(), Block._____(), Block._____(), Block._E___(), Block._E_WC(),
        //3
        Block.___W_(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(),
        Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._E___(), Block._E_WC(),
        //4
        Block.__SW_(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(),
        Block._____(), Block._____(), Block._____(), Block.__S__(), Block.__S__(), Block.__S__(), Block._____(), Block._____(), Block._E___(), Block._E_WC(),
        //5
        Block.N__WC(), Block.N___C(), Block.N___C(), Block.N___C(), Block.N___C(), Block.N___C(), Block.N___C(), Block.N___C(), Block.N___C(), Block.NE__C(),
        Block.___W_(), Block._____(), Block._E___(), Block.N__WC(), Block.N___C(), Block.NE__C(), Block.___W_(), Block._____(), Block._E___(), Block._E_WC(),
        //6
        Block.___WC(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block._E__C(),
        Block.___W_(), Block._____(), Block._E___(), Block.___WC(), Block.____C(), Block._E__C(), Block.___W_(), Block._____(), Block._E___(), Block._E_WC(),
        //7
        Block.___WC(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block.____C(), Block._E__C(),
        Block.___W_(), Block._____(), Block._E___(), Block.___WC(), Block.____C(), Block._E__C(), Block.___W_(), Block._____(), Block._E___(), Block._E_WC(),
        //8
        Block.__SWC(), Block.__S_C(), Block.__S_C(), Block.__S_C(), Block.__S_C(), Block.__S_C(), Block.__S_C(), Block.__S_C(), Block.__S_C(), Block._ES_C(),
        Block.___W_(), Block._____(), Block._E___(), Block.__SWC(), Block.__S_C(), Block._ES_C(), Block.___W_(), Block._____(), Block._E___(), Block._ESWC(),
        //9
        Block.N__W_(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(), Block.N____(),
        Block._____(), Block.__S__(), Block._____(), Block.N____(), Block.N____(), Block.N____(), Block._____(), Block.__S__(), Block._____(), Block.NE___(),
        //10
        Block.___W_(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(),
        Block._E___(), Block.NESWC(), Block.___W_(), Block._____(), Block._____(), Block._____(), Block._E___(), Block.NESWC(), Block.___W_(), Block._E___(),
        //11
        Block.__SW_(), Block.__S__(), Block.__S__(), Block.__S__(), Block._____(), Block._____(), Block._____(), Block.__S__(), Block.__S__(), Block.__S__(),
        Block._____(), Block.N____(), Block._____(), Block.__S__(), Block.__S__(), Block.__S__(), Block._____(), Block.N____(), Block._____(), Block._ES__(),
        //12
        Block.N__WC(), Block.N___C(), Block.N___C(), Block.NE__C(), Block.___W_(), Block._____(), Block._E___(), Block.N__WC(), Block.N___C(), Block.NE__C(),
        Block.___W_(), Block._____(), Block._E___(), Block.N__WC(), Block.N___C(), Block.NE__C(), Block.___W_(), Block._____(), Block._E___(), Block.NE_WC(),
        //13
        Block.___WC(), Block.____C(), Block.____C(), Block._E__C(), Block.___W_(), Block._____(), Block._E___(), Block.___WC(), Block.____C(), Block._E__C(),
        Block.___W_(), Block._____(), Block._E___(), Block.___WC(), Block.____C(), Block._E__C(), Block.___W_(), Block._____(), Block._E___(), Block._E_WC(),
        //14
        Block.___WC(), Block.____C(), Block.____C(), Block._E__C(), Block.___W_(), Block._____(), Block._E___(), Block.___WC(), Block.____C(), Block._E__C(),
        Block.___W_(), Block._____(), Block._E___(), Block.___WC(), Block.____C(), Block._E__C(), Block.___W_(), Block._____(), Block._E___(), Block._E_WC(),
        //15
        Block.___WC(), Block.____C(), Block.____C(), Block._E__C(), Block.___W_(), Block._____(), Block._E___(), Block.__SWC(), Block.__S_C(), Block._ES_C(),
        Block.___W_(), Block._____(), Block._E___(), Block.___WC(), Block.____C(), Block._E__C(), Block.___W_(), Block._____(), Block._ES__(), Block._ESWC(),
        //16
        Block.__SWC(), Block.__S_C(), Block.__S_C(), Block._ES_C(), Block.___W_(), Block._____(), Block._____(), Block.N____(), Block.N____(), Block.N____(),
        Block._____(), Block._____(), Block._ES__(), Block.___WC(), Block.____C(), Block._ES_C(), Block.___W_(), Block._E___(), Block.NESWC(), Block.NE_W_(),
        //17
        Block.N__W_(), Block.N____(), Block.N____(), Block.N____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(),
        Block._____(), Block._E___(), Block.N_SWC(), Block.____C(), Block._ES_C(), Block.N__W_(), Block._____(), Block._____(), Block.N____(), Block._E___(),
        //18
        Block.___W_(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(), Block._____(),
        Block._____(), Block._____(), Block.NE___(), Block._ESWC(), Block.N__W_(), Block._____(), Block._____(), Block.__S__(), Block.__S__(), Block._ES__(),
        //19
        Block.__SW_(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block._____(), Block._____(), Block._____(),
        Block._____(), Block._____(), Block._____(), Block.N____(), Block._____(), Block._____(), Block._E___(), Block.N__WC(), Block.N___C(), Block.NE__C(),
        //20
        Block.N_SWC(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.N_S_C(), Block.NES_C(), Block.__SW_(), Block.__S__(), Block.__S__(),
        Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block.__S__(), Block._ES__(), Block.__SWC(), Block.__S_C(), Block._ES_C()
    };

    public Road() {
        roadColor = Color.BLACK;
        gridLineColor = Color.GRAY;
        obstableColor = new Color(136, 168, 104);
        asphalt = Color.DARK_GRAY;
        scrsize = nrofblocks * blocksize;
        ImageIcon ii = new ImageIcon(this.getClass().getResource(grassSrc));
        grass = ii.getImage().getScaledInstance(blocksize, blocksize, Image.SCALE_SMOOTH);

        screendata = new int[nrofblocks * nrofblocks][5];
        levelInit();
    }

    public static int getNrofblocks() {
        return nrofblocks;
    }

    public static int getBlockSize() {
        return blocksize;
    }

    public int[][] getScreenData() {
        return screendata;
    }

    private void levelInit() {
        screendata = leveldata2.clone();
    }

    public void DrawMaze(Graphics2D g2d) {
        short i = 0;
        int x, y;

        for (y = 0; y < scrsize; y += blocksize) {
            for (x = 0; x < scrsize; x += blocksize) {
                g2d.setStroke(new BasicStroke(2));
                //road inkleuren
                if (screendata[i][4] == 0) {
                    g2d.setColor(asphalt);
                } //obstacle inkleuren
                else if (screendata[i][4] == 1) {
                    g2d.setColor(obstableColor);
                } //passed route coloring
                else if (screendata[i][4] == 2) {
                    g2d.setColor(Color.BLUE);
                } else if (screendata[i][4] == 3) {
                    g2d.setColor(Color.RED);
                }
                g2d.fillRect(x, y, blocksize, blocksize);

                if (screendata[i][4] == 1) {
                    g2d.drawImage(grass, x, y, null);
                }
                //lines
                //N
                if (screendata[i][0] == 0) {
                    g2d.setColor(gridLineColor);
                    g2d.drawLine(x, y, x + blocksize - 1, y);

                } else if (screendata[i][0] == 1) {
                    g2d.setColor(roadColor);
                    g2d.drawLine(x, y, x + blocksize - 1, y);
                }
                //E
                if (screendata[i][1] == 0) {
                    g2d.setColor(gridLineColor);
                    g2d.drawLine(x + blocksize - 1, y, x + blocksize - 1, y + blocksize - 1);
                } else if (screendata[i][1] == 1) {
                    g2d.setColor(roadColor);
                    g2d.drawLine(x + blocksize - 1, y, x + blocksize - 1, y + blocksize - 1);
                }
                //S
                if (screendata[i][2] == 0) {
                    g2d.setColor(gridLineColor);
                    g2d.drawLine(x, y + blocksize - 1, x + blocksize - 1, y + blocksize - 1);
                } else if (screendata[i][2] == 1) {
                    g2d.setColor(roadColor);
                    g2d.drawLine(x, y + blocksize - 1, x + blocksize - 1, y + blocksize - 1);
                }

                //W
                if (screendata[i][3] == 0) {
                    g2d.setColor(gridLineColor);
                    g2d.drawLine(x, y, x, y + blocksize - 1);
                } else if (screendata[i][3] == 1) {
                    g2d.setColor(roadColor);
                    g2d.drawLine(x, y, x, y + blocksize - 1);
                }

                i++;
            }//inner for
        }//outer for
    }
}